package com.tang.netty.nio.server;

import com.tang.netty.nio.ChannelAdapter;
import com.tang.netty.nio.ChannelHandler;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.charset.Charset;
import java.time.LocalDateTime;

/**
 * 消息处理器
 */
@Slf4j
public class NioServerHandler extends ChannelAdapter {

    public NioServerHandler(Selector selector, Charset charset) {
        super(selector, charset);
    }

    @Override
    public void channelActive(ChannelHandler channelHandler) {
        try {
            log.info("连接地址：{}", channelHandler.channel().getLocalAddress());
            channelHandler.writeAndFlush("Nio message for you!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void channelRead(ChannelHandler channelHandler, Object msg) {
        log.info("{} 接收到消息 {}", LocalDateTime.now(), msg);
        channelHandler.writeAndFlush("Hi! we have receive your message!");
    }
    
}
